﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZDT.Common;

namespace ZDT.Admin
{
    public partial class GatherForm : Form
    {
        public GatherForm()
        {
            InitializeComponent();
        }
        public string FilePath;
        public ZDT.Task.Entity.GatherInfo gatherInfo = null;
        private void startBtn_Click(object sender, EventArgs e)
        {
        }

        private void goBtn_Click(object sender, EventArgs e)
        {
            if(!urlTxt.Text.ToLower().StartsWith("http"))
            {
                urlTxt.Text = "http://" + urlTxt.Text;
            }
            myWebBrowser1.Url = new Uri(urlTxt.Text);
        }

        private void htmlChk_CheckedChanged(object sender, EventArgs e)
        {
            htmlTxt.Visible = htmlChk.Visible;
            myWebBrowser1.Visible = !htmlChk.Visible;
        }

        private void myWebBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            urlTxt.Text = e.Url.ToString();
            htmlTxt.Text = myWebBrowser1.DocumentText;
        }

        private void GatherForm_Load(object sender, EventArgs e)
        {
            Init();
            startBtn.PerformClick();
            Type type = typeof(ZDT.Task.Entity.GatherInfo);
            objTrv.Nodes.Add("gatherInfo " + type.Name);
            RenderNode(type, objTrv.TopNode);
            objTrv.ExpandAll();
        }
        private void RenderNode(Type type, TreeNode parentNode)
        {
            foreach (var item in type.GetProperties())
            {
                if (item.PropertyType.IsClass && !item.PropertyType.IsSealed)
                {
                    parentNode.Nodes.Add(item.Name + " " + item.PropertyType.Name);
                    RenderNode(item.PropertyType, parentNode.LastNode);
                }
            }
        }

        private void objTrv_AfterSelect(object sender, TreeViewEventArgs e)
        {
        }

        private void objTrv_Enter(object sender, EventArgs e)
        {
            objTrv.Height = 25 * 10;
            gatherPgd.Top = 50 + 25 * 10;
        }

        private void objTrv_Leave(object sender, EventArgs e)
        {
            objTrv.Height = 25 * 5;
            gatherPgd.Top = 50 + 25 * 5;
        }

        private void Init()
        {
            if (gatherInfo == null)
            {
                if (string.IsNullOrEmpty(FilePath))
                {
                    gatherInfo = new Task.Entity.GatherInfo();
                    if (gatherInfo.LoginInfo == null)
                    {
                        gatherInfo.LoginInfo = new Task.Entity.LoginInfo();
                    }
                    if (gatherInfo.DetailInfo == null)
                    {
                        gatherInfo.DetailInfo = new Task.Entity.DetailInfo();
                        gatherInfo.DetailInfo.Rules = new SDictionary<string, Task.Entity.GatherRule>();
                    }
                    if (gatherInfo.ListPageInfo == null)
                    {
                        gatherInfo.ListPageInfo = new Task.Entity.ListPageInfo();
                    }
                    if (gatherInfo.PublishInfo == null)
                    {
                        gatherInfo.PublishInfo = new Task.Entity.PublishInfo();
                        gatherInfo.PublishInfo.Datas = new List<Task.Data.DetailItemInfo>();
                        gatherInfo.PublishInfo.WebInfos = new SDictionary<string, Task.Entity.PublishWebInfo>();
                    }
                    FilePath = string.Format(@"Config\Admin\{0}.xml", ZDT.Common.Utility.NewName());
                }
                else
                {
                    gatherInfo = X2S.FileToObj<ZDT.Task.Entity.GatherInfo>(FilePath);
                }
            }
            gatherPgd.SelectedObject = gatherInfo;
        }
        private void objTrv_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            switch (e.Node.Text)
            {
                case "gatherInfo GatherInfo":
                    gatherPgd.SelectedObject = gatherInfo;
                    break;
                case "LoginInfo LoginInfo":
                    if (gatherInfo.LoginInfo == null)
                    {
                        gatherInfo.LoginInfo = new Task.Entity.LoginInfo();
                    }
                    gatherPgd.SelectedObject = gatherInfo.LoginInfo;
                    break;
                case "DetailInfo DetailInfo":
                    if (gatherInfo.DetailInfo == null)
                    {
                        gatherInfo.DetailInfo = new Task.Entity.DetailInfo();
                    }
                    gatherPgd.SelectedObject = gatherInfo.DetailInfo;
                    break;
                case "ListPageInfo ListPageInfo":
                    if (gatherInfo.ListPageInfo == null)
                    {
                        gatherInfo.ListPageInfo = new Task.Entity.ListPageInfo();
                    }
                    gatherPgd.SelectedObject = gatherInfo.ListPageInfo;
                    break;
                case "PublishInfo PublishInfo":
                    if (gatherInfo.PublishInfo == null)
                    {
                        gatherInfo.PublishInfo = new Task.Entity.PublishInfo();
                    }
                    gatherPgd.SelectedObject = gatherInfo.PublishInfo;
                    break;
                case "Datas List 1":
                    gatherPgd.SelectedObject = gatherInfo.PublishInfo.Datas;
                    break;
                case "WebInfos SDictionary 2":
                    gatherPgd.SelectedObject = gatherInfo.PublishInfo.Datas;
                    break;
                case "BaseRule GatherRule":  
                    gatherPgd.SelectedObject = gatherInfo.DetailInfo.BaseRule;
                    break;
                case "ImgItemRule GatherRule":
                    gatherPgd.SelectedObject = gatherInfo.DetailInfo.ImgItemRule;
                    break;
                case "ImgRule GatherRule":
                    gatherPgd.SelectedObject = gatherInfo.DetailInfo.ImgRule;
                    break;
                case "Rules SDictionary`2":
                    gatherPgd.SelectedObject = gatherInfo.DetailInfo.Rules;
                    break;
                default:
                    break;
            }
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            X2S.ObjToFile<ZDT.Task.Entity.GatherInfo>(gatherInfo, FilePath);
        }
    }
}
